package com.sunjq.fetchdata.internet;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.sunjq.fetchdata.object.Notice;

public class ParseDataWaiShiChu extends BaseParseData {

	private String baseUrl = "http://www.fao.fudan.edu.cn";

	private String homeUrl = "http://www.fao.fudan.edu.cn/s/42/t/88/p/12/i/1/list.htm";

	public ParseDataWaiShiChu(String url) {
		super(url);
		// this.genDataFromUrl(url);

	}

	public ParseDataWaiShiChu() {
		super();
		// this.genDataFromUrl(homeUrl);
	}

	@Override
	public List<Notice> buildData() {
		// System.out.println("start build data");
		this.genDataFromUrl(homeUrl);
		List<Notice> resultList = this.matchAll();
		// System.out.println("end build data");

		
		Collections.reverse(resultList);
		return resultList;
	}

	public List<Notice> buildAllData(int looptime) {
		List<Notice> resultList = new ArrayList<Notice>();
		for (int i = 1; i <= looptime; i++) {
			String url = "http://www.fao.fudan.edu.cn/s/42/t/88/p/12/i/" + i
					+ "/list.htm";
			this.genDataFromUrl(url);
			List<Notice> temp1 = this.matchAll();
			resultList.addAll(temp1);
		}
		
		
		Collections.reverse(resultList);
		
		return resultList;
	}

	protected String match(String content, String left, String right, int index) {
		// System.out.println("conetnet ==> " + content);
		Pattern patternLeft = Pattern.compile(left);
		String[] temp = patternLeft.split(content);

		int matchCount = 0;
		for (int i = 0; i < temp.length; i++) {

			Pattern patternRight = Pattern.compile(right);

			if (temp[i].matches(".*" + right + ".*")) {

				String[] temp1 = patternRight.split(temp[i]);

				matchCount++;
				if (matchCount == index) {
					return temp1[0];
				}
			}
		}

		return null;
	}

	private List<Notice> matchAll(String content, String left, String right) {
		// System.out.println("conetnet ==> " + content);

		Pattern patternLeft = Pattern.compile(left);
		String[] temp = patternLeft.split(content);

		for (int i = 0; i < temp.length; i++) {

			Pattern patternRight = Pattern.compile(right);
			// System.out.println("======================");
			// System.out.println("temp " + i + " -> " + temp[i]);
			// System.out.println("======================");

			if (temp[i].indexOf(right) >= 0) {
				// if (temp[i].matches(".*" + right + ".*")) {

				Notice notice = new Notice();
				String[] temp1 = patternRight.split(temp[i]);
				// System.out.println(">>>>>>>>>1<<<<<<<");
				// System.out.println(temp1[0]);
				// System.out.println(">>>>>>>>>2<<<<<<<");

				String[] temp2 = temp1[0].split("' target='_blank'  style=''>");
				notice.setTitle(temp2[0]);
				if (temp2[1].indexOf("'>") >= 0) {
					// System.out.println("temp2[1] ==> " + temp2[1]);
					String temp3 = temp2[1];

					Pattern p = Pattern.compile("</font>");
					Matcher m = p.matcher(temp3);
					temp2[1] = m.replaceAll("");
					// System.out.println("new temp2[1] ==> " + temp2[1]);
					temp3 = temp2[1];

					Pattern p1 = Pattern.compile("</b>");
					Matcher m1 = p1.matcher(temp3);
					temp2[1] = m1.replaceAll("");
					// System.out.println("new 1 temp2[1] ==> " + temp2[1]);
					temp3 = temp2[1];

					Pattern p2 = Pattern.compile("<font.*>");
					Matcher m2 = p2.matcher(temp3);
					temp2[1] = m2.replaceAll("");
					// System.out.println("new 2 temp2[1] ==> " + temp2[1]);

				}

				notice.setDetailContentUrl(this.baseUrl + temp2[0]);
				notice.setTitle(temp2[1]);
				noticeList.add(notice);

			}
		}

		// for (int index =0; index < noticeList.size(); index++) {
		// System.out.println("===========" + index + " start =====");
		// System.out.println(noticeList.get(index).getTitle());
		// System.out.println(noticeList.get(index).getDetailContentUrl());
		// System.out.println("===========" + index + " end =====");
		// }

		return noticeList;
	}

	private List<Notice> matchAll() {

		// System.out.println("start => " + this.getHttpResponseBody());
		this.matchAll(this.getHttpResponseBody(), "<tr><td><a href='",
				"</a></td></tr></table>");
		return noticeList;

	}

}
